package com.atguigu.yygh.user.api;

import com.atguigu.yygh.commom.result.Result;
import com.atguigu.yygh.commom.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author mengWu
 * @Date 2021年05月28日 10:27
 */
@Api(tags = "就诊人管理")
@RestController
@RequestMapping("api/user/patient")
public class PatientApiController {
    @Autowired
    private PatientService patientService;
    //根据就诊人的id获取就诊人的信息
    @ApiOperation(value = "根据就诊人的id获取就诊人的信息")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id){
        Patient patient = patientService.getPatientId(id);

        return patient;
    }
    //获取就诊人列表
    @ApiOperation(value = "获取就诊人列表")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        //获取当前登录用户的id
        long userId = getUserId(request);
        List<Patient> list = patientService.finaAllUserId(userId);
        return Result.ok(list);

    }

    //添加就诊人
    @ApiOperation(value = "添加就诊人")
    @PostMapping("auth/save")
    public Result  savePatient(@RequestBody Patient patient, HttpServletRequest request){
        //获取当前登录用户的id
        long userId = getUserId(request);
        patient.setUserId(userId);

        patientService.save(patient);

        return Result.ok();
    }
    //获取当前登录用户的id
    private long getUserId(HttpServletRequest request) {
        long userId = AuthContextHolder.getUserId(request);
        return userId;
    }
    //根据id获取就诊人信息
    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable long id){
        Patient patient = patientService.getPatientId(id);

        return Result.ok(patient);
    }
    //修改就诊人信息
    @ApiOperation(value = "修改就诊人信息")
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }
    //删除就诊人
    @ApiOperation(value = "删除就诊人")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable long id){
        patientService.removeById(id);
        return Result.ok();
    }
}
